package com.bs.modules.use.controller;

import java.util.Arrays;
import java.util.Map;

import com.bs.modules.use.entity.AppPriceEntity;
import com.bs.modules.use.service.AppPriceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bs.modules.use.entity.AppWaterEntity;
import com.bs.modules.use.service.AppWaterService;
import com.bs.common.utils.PageUtils;
import com.bs.common.utils.R;


/**
 * 水表
 *
 * @author lichenguang
 * @email 13031069255@163.com
 * @date 2022-04-05 20:38:56
 */
@RestController
@RequestMapping("use/appwater")
@Api(tags = "水表")
public class AppWaterController {

  @Autowired
  private AppWaterService appWaterService;

  @Autowired
  private AppPriceService appPriceService;
  @ApiOperation(value = "列表")
  @GetMapping("/list")
  @RequiresPermissions("use:appwater:list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = appWaterService.queryPage(params);

    return R.ok().put("page", page);
  }


  @ApiOperation(value = "详情")
  @GetMapping("/info/{id}")
  @RequiresPermissions("use:appwater:info")
  public R info(@PathVariable("id") Long id) {
      AppWaterEntity appWater = appWaterService.getById(id);

    return R.ok().put("appWater", appWater);
  }

  @ApiOperation(value = "保存")
  @PostMapping("/save")
  @RequiresPermissions("use:appwater:save")
  public R save(@RequestBody AppWaterEntity appWater) {
    AppPriceEntity appPriceEntity = appPriceService.findNewInfo();
    if (appPriceEntity == null) {
      return R.error("价格为空请联系管理员");
    }

      appWater.setWaterStatus("1");

    //获取这个月水表数
    int waterCoun = Integer.parseInt( appWater.getWaterCount());

    //获取上个月水表数
    int waterBeforecount = Integer.parseInt(appWater.getWaterBeforecount());

    // 这个月 -  上个月  = 用的水/吨
    int sum  = waterCoun - waterBeforecount;
    appWater.setWaterNumber(String.valueOf(sum));
    //根据电表价格id 价格查对应
    int  powerPriceid = Integer.parseInt(appWater.getWaterPriceid());
    Long b  = (long) powerPriceid;
    AppPriceEntity byId = appPriceService.getById(b);
    //水价格
    Double powprice = Double.valueOf(byId.getWaterprice());
    Double  money =  sum * powprice;
    System.out.println(money);

    appWater.setWaterMoneyone(String.format("%.2f", money));

      appWaterService.save(appWater);

    return R.ok();
  }


  @ApiOperation(value = "修改")
  @PostMapping("/update")
  @RequiresPermissions("use:appwater:update")
  public R update(@RequestBody AppWaterEntity appWater) {
      appWaterService.updateById(appWater);

    return R.ok();
  }

  @ApiOperation(value = "删除")
  @PostMapping("/delete")
  @RequiresPermissions("use:appwater:delete")
  public R delete(@RequestBody Long[] ids) {
      appWaterService.removeByIds(Arrays.asList(ids));

    return R.ok();
  }

}
